package cn.csc.module.member.dal.mysql.user;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.member.controller.admin.user.vo.MemberUserExportReqVO;
import cn.csc.module.member.controller.admin.user.vo.MemberUserInfoRespVO;
import cn.csc.module.member.controller.admin.user.vo.MemberUserPageReqVO;
import cn.csc.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO;
import cn.csc.module.member.dal.dataobject.user.MemberUserDO;
import cn.csc.module.system.api.social.dto.SocialUserBindRespDTO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 会员 User Mapper
 *
 * @author 
 */
@Mapper
public interface MemberUserMapper extends BaseMapperX<MemberUserDO> {

    default PageResult<MemberUserDO> selectPage(MemberUserPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MemberUserDO>()
                .likeIfPresent(MemberUserDO::getNickname, reqVO.getNickname())
                .eqIfPresent(MemberUserDO::getIdCardType, reqVO.getIdCardType())
                .eqIfPresent(MemberUserDO::getIdCardNo, reqVO.getIdCardNo())
                .likeIfPresent(MemberUserDO::getName, reqVO.getName())
                .eqIfPresent(MemberUserDO::getStatus, reqVO.getStatus())
                .eqIfPresent(MemberUserDO::getGender, reqVO.getGender())
                .eqIfPresent(MemberUserDO::getBirthday, reqVO.getBirthday())
                .eqIfPresent(MemberUserDO::getAreaId, reqVO.getAreaId())
                .eqIfPresent(MemberUserDO::getMark, reqVO.getMark())
                .eqIfPresent(MemberUserDO::getScore, reqVO.getScore())
                .eqIfPresent(MemberUserDO::getAvatar, reqVO.getAvatar())
                .eqIfPresent(MemberUserDO::getMobile, reqVO.getMobile())
                .eqIfPresent(MemberUserDO::getEmail, reqVO.getEmail())
                .eqIfPresent(MemberUserDO::getPassword, reqVO.getPassword())
                .eqIfPresent(MemberUserDO::getRegisterType, reqVO.getRegisterType())
                .eqIfPresent(MemberUserDO::getType, reqVO.getType())
                .eqIfPresent(MemberUserDO::getOpenId, reqVO.getOpenId())
                .eqIfPresent(MemberUserDO::getUnionId, reqVO.getUnionId())
                .eqIfPresent(MemberUserDO::getToken, reqVO.getToken())
                .eqIfPresent(MemberUserDO::getPayPassword, reqVO.getPayPassword())
                .eqIfPresent(MemberUserDO::getRegisterIp, reqVO.getRegisterIp())
                .eqIfPresent(MemberUserDO::getLoginIp, reqVO.getLoginIp())
                .betweenIfPresent(MemberUserDO::getLoginDate, reqVO.getLoginDate())
                .betweenIfPresent(MemberUserDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MemberUserDO::getId));
    }

    default List<MemberUserDO> selectExport(MemberUserExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<MemberUserDO>()
                .eqIfPresent(MemberUserDO::getStatus, reqVO.getStatus())
                .eqIfPresent(MemberUserDO::getMobile, reqVO.getMobile())
                .eqIfPresent(MemberUserDO::getEmail, reqVO.getEmail())
                .eqIfPresent(MemberUserDO::getType, reqVO.getType())
                .betweenIfPresent(MemberUserDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MemberUserDO::getId));
    }

    default MemberUserDO selectByMobile(String mobile) {
        return selectOne(MemberUserDO::getMobile, mobile);
    }


    MemberUserInfoRespVO selectByMobileWithRoles(String mobile);


    default List<MemberUserDO> selectListByNicknameLike(String nickname) {
        return selectList(new LambdaQueryWrapperX<MemberUserDO>()
                .likeIfPresent(MemberUserDO::getNickname, nickname));
    }

    /**
     * 部分更新用户信息
     * @param respDTO 社交用户绑定返回
     * @return
     */
    default Integer updateUserInfo(SocialUserBindRespDTO respDTO, String password, String unionId){
        LambdaUpdateWrapper<MemberUserDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(MemberUserDO::getOpenId, respDTO.getOpenid());
        userUpdateWrapper.set(MemberUserDO::getUnionId, unionId);
        userUpdateWrapper.set(MemberUserDO::getPassword, password);
        userUpdateWrapper.eq(MemberUserDO::getId, respDTO.getUserId());
        return update(null, userUpdateWrapper);
    }

    /**
     * 部分更新用户信息
     * @param userId 用户编号
     * @param hzhUserId  华智慧的userId
     * @return
     */
    default Integer updateUserInfo(Long userId, Long hzhUserId){
        LambdaUpdateWrapper<MemberUserDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(MemberUserDO::getId, hzhUserId);
        userUpdateWrapper.eq(MemberUserDO::getId, userId);
        return update(null, userUpdateWrapper);
    }

    default Integer modify(AppMemberUserUpdateReqVO reqVO, Long userId){
        LambdaUpdateWrapper<MemberUserDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(StringUtils.hasLength(reqVO.getNickname()), MemberUserDO::getNickname, reqVO.getNickname());
        userUpdateWrapper.set(reqVO.getBirthday() != null , MemberUserDO::getBirthday, reqVO.getBirthday());
        userUpdateWrapper.set(StringUtils.hasLength(reqVO.getAvatar()), MemberUserDO::getAvatar, reqVO.getAvatar());
        userUpdateWrapper.eq(MemberUserDO::getId, userId);
        return update(null, userUpdateWrapper);
    }

    default Integer updateType(Integer type, Long userId){
        LambdaUpdateWrapper<MemberUserDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set( MemberUserDO::getType, type);
        userUpdateWrapper.eq(MemberUserDO::getId, userId);
        return update(null, userUpdateWrapper);
    }



}
